define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/index',
                    add_url: '',
                    edit_url: '',
                    del_url: 'member/reload',
                    multi_url: 'member/group',
                }
            });
            var table = $("#table");
            var groupList = groups || []; 
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: 'ID',operate:false},
                        {field: 'headimgurl',width:"45px" ,title: __('头像'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'nickname', title: __('昵称'),align:'left'},
                        {field: 'groupid', title: __('用户组'),align:'left',formatter: Table.api.formatter.status,searchList: groupList},
                        {field: 'country', title: __('国家'),align:'left',operate:false},
                        {field: 'city', title: __('城市'), operate:false,align:'left'},
                        {field: 'subscribe_scene',align:'left', title: __("渠道来源"),operate:false, searchList: {"1":__('订阅号'),"3":__('服务号')}, formatter: Table.api.formatter.status},
                        {field: 'subscribe_time', title: __('关注时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'update_at', title: __('更新时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate',width:"125px",align:'right', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                    name: 'del',
                                    text: '',
                                    title:'更新',
                                    icon: 'fa fa-refresh',
                                    classname: 'btn btn-danger btn-xs btn-delone',
                                }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 下载
            $("body").on('click',".btn-download",function(){
                var that = $(this)
                if(that.hasClass('disabled')){
                    return false;
                }
                if(!confirm('确定下载所有用户吗?')){
                    return false;
                }
                that.addClass('disabled');
                var layerIndex = Layer.load(1)
               
                $.post(that.data('href'),{},function(ret){
                    Layer.close(layerIndex)
                    that.removeClass('disabled');
                    Toastr.success(ret.msg);
                })
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        }
        
    };
    return Controller;
});
